<template>
  <div class="h100 strategyPage tablePageWarp">
    <full-parent-dom-with-dask :tip="operationTip" :loading="spinningLoading"></full-parent-dom-with-dask>
    <div class="strategySearch table-page-search-wrapper">
      <strategy-search @clickbtn="handleClickSearchBtn"
                       :version-type="versionType" v-model="queryParams"
                       @change="$nextTick(()=>searchData(true))"
                       :upgrade-trigger-type="upgradeTriggerType"
      />
    </div>
    <div style="" v-if="versionType !== 'develop'">
      <a-radio-group buttonStyle="solid" v-model="queryParams.fromGcy" style="float:right;margin-bottom: 8px" @change="searchData(true)">
        <a-radio-button :value="false">
          规划院
        </a-radio-button>
        <a-radio-button :value="true">
          工程院
        </a-radio-button>
      </a-radio-group>
    </div>
    <div class="strategyMain">
      <strategy-table-list @clickbtn="handleMainCompClick" ref="strategyTableList"
                           :version-type="versionType" :queryParams="queryParams"
                           :upgrade-trigger-type="upgradeTriggerType"
      />
    </div>
  </div>
</template>
<script>
import StrategySearch from './components/StrategySearch.vue'
import StrategyTableList from './components/StrategyTableList.vue'
import EditStrategy from './components/EditStrategy'
import StrategyDetail from './components/StrategyDetail'

import FullParentDomWithDask from '@/components/LoadDataSpin/FullParentDomWithDask.vue'
import { minxinDrawerUse } from '@/utils/mixin'
import { strategyPerms } from '@/config/permission/index'
import StrategyJs from '../strategy'

export default {
  mixins: [minxinDrawerUse],
  components: {
    StrategySearch,
    StrategyTableList,
    EditStrategy,
    StrategyDetail,
    FullParentDomWithDask
  },
  provide() {
    return {
      queryParams: this.queryParams
    }
  },
  data () {
    return {
      queryParams : {
          type: 1,
          name: '',
          status: undefined,
          createBy: undefined,
          vehicleVersionId: undefined,
          fromGcy: false
      },
      nowTime: 123456,
      spinningLoading: false,
      versionType : StrategyJs.getTypeFromPath(this.$route.path, 1, this),
      upgradeTriggerType :'USER',
      operationTip: ''
    }
  },
  computed: {
  },
  created () {
  },
  mounted () {
  },
  methods: {
    handleMainCompClick (obj) {
      if (this[obj.type]) {
        this[obj.type](obj.data)
      }
    },
    handleClickSearchBtn (data) {
      this.nowTime = new Date().getTime()
      if (this[data.type]) {
        this[data.type](data)
      }
    },
    // 添加FOTA条件策略
    addStrategy () {
      const name = this.$t('tjcl')
      const vars = {
        time: Date.now(),
        id: '',
        class: 'rightDrawerWithoutCol',
        versionType:this.versionType,
        upgradeTriggerType:this.upgradeTriggerType
      }
      const buttons = this.getTjclDrawerButtons(strategyPerms[this.versionType][this.upgradeTriggerType].fotaTjcl.add)
      const title = this.$t('new') + StrategyJs.getTestTypeDesc(this.versionType) + name;
      this.showRightDrawerNoReset(EditStrategy, title, buttons, 'large', vars)
    },
    // 编辑FOTA条件策略
    editStrategy (record, isCopy) {
      const vars = {
        time: Date.now(),
        id: record.id,
        isCopy,
        class: 'rightDrawerWithoutCol',
        versionType:this.versionType,
        upgradeTriggerType:this.upgradeTriggerType
      }
      const buttons = this.getTjclDrawerButtons(strategyPerms[this.versionType][this.upgradeTriggerType].fotaTjcl.edit)
      const title = this.$t(isCopy ? 'copy' : 'edit') + this.$t('tjcl')
      this.showRightDrawer(EditStrategy, title, buttons, 'large', vars)
    },
    // 复制策略
    copyStrategy (data) {
      this.editStrategy(data, true)
    },
    // 获取编辑，新建条件策略时右侧弹框按钮
    getTjclDrawerButtons (auth) {
      const _this = this
      return this.getRightDrawerFooterButtons({
        key: auth,
        method: 'saveEditStrategyFormData',
        callback (data) {
          _this.searchData(!data.id)
        }
      })
    },
    // 保存条件策略表单
    saveEditStrategyFormData (data) {
      if (data.id === 'copy') {
        // eslint-disable-next-line no-param-reassign
        data.id = null
      }
      const method = data.id ? 'editStrategyData' : 'addStrategyData'
      console.log(data, 'data')
      return this.$store.dispatch(method, data)
    },
    // 查看策略详情
    lookupRowDetail (data) {
      const vars = {
        id: data.id,
        time: Date.now(),
        versionType:this.versionType,
        upgradeTriggerType:this.upgradeTriggerType
      }
      this.showRightDrawer(StrategyDetail, data.name, [], 'large', vars)
    },
    // 批量删除
    deleteList (record) {
      const vm = this
      this.confirmAjax({
        content: this.$t('deleteConfirmNotice').replace('__CONTENT__', `${this.$t('strategy')}[${record.name}]`),
        title: vm.$t('delete') + vm.$t('strategy'),
        loadingTip: `${vm.$t('delete')}[${record.name}]`,
        method: 'deleteStrategyData',
        data: {ids:[record.id], versionType: this.versionType, upgradeTriggerType:this.upgradeTriggerType}
      })
    },
    // 停用策略
    disabledStrategy (record) {
      const vm = this
      this.confirmAjax({
        content: vm.$t('disabledConfirmNotice').replace('__CONTENT__', `${vm.$t('strategy')}[${record.name}]?`),
        title: vm.$t('disabled') + vm.$t('strategy'),
        loadingTip: `${vm.$t('disabled')}[${record.name}]`,
        method: 'disabledStrategyData',
        data: { ids: [record.id], enabled: false, versionType: this.versionType, upgradeTriggerType:this.upgradeTriggerType }
      })
    },
    // 启用策略
    enabledStrategy (record) {
      const vm = this
      this.confirmAjax({
        content: vm.$t('enabledConfirmNotice').replace('__CONTENT__', `${vm.$t('strategy')}[${record.name}]?`),
        title: vm.$t('start') + vm.$t('strategy'),
        loadingTip: `${vm.$t('start')}[${record.name}]`,
        method: 'disabledStrategyData',
        data: { ids: [record.id], enabled: true, versionType: this.versionType, upgradeTriggerType:this.upgradeTriggerType }
      })
    },
    changeOperationTip (newCont, oldCont = '__CONTENT__') {
      this.operationTip = this.operationTip.replace(oldCont, newCont)
    },
    confirmAjax (obj = { title: '', content: '', loadingTip: '', method: '', data: null, callback: null }) {
      if (this.spinningLoading) {
        return
      }
      const vm = this
      const { content } = obj
      vm.$confirm({
        title: obj.title,
        content,
        class: 'confirmStyle',
        width: 378,
        onOk () {
          vm.spinningLoading = true
          const newCont = obj.loadingTip
          vm.changeOperationTip(newCont)
          vm.$store.dispatch(obj.method, obj.data).then(() => {
            if (obj.callback) {
              obj.callback()
              return
            }
            vm.$message.success(obj.title + vm.$t(200))
            vm.searchData(false)
          }).finally(() => {
            vm.changeOperationTip('__CONTENT__', newCont)
            vm.spinningLoading = false
          })
        },
        onCancel () { }
      })
    },
    searchData (isPageOne = true) {
      this.$refs.strategyTableList.refreshData(isPageOne)
    }
  }
}
</script>
</template>
<style scoped lang="less">
@import url('../style/list.less');
</style>
